<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 01.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class pageInserter
    ****
    * This class maps files to nodes/directories on insertion
    ****
    * @package usefulStuff
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 01.06.2005
    * @copyright essentialmind.com 2005
    ***/
    class pageInserter{
        
        /**
         * Reference to the open database connection
         *
         * @var ADOConnection
         */
        protected $db;
        
        protected $log;

        
        /**
         * Constructor
         *
         * @param ADOConnection $db
         */
        public function __construct(ADOConnection $db, $log=null){
            $this->db   = $db;
            $this->log  = $log;
        }
        
        /**
         * This function handles the insertion of an entry into the pagemap table, which 
         * efficiently maps end files with nodes/direcotries. The purpose of outsourcing
         * this procedure into a separate class is that all the permissions and workflow
         * states are then set automatically, thus leaving more space for the imagination
         * of the people extending the framework
         *
         * @param integer   $nodeID
         * @param integer   $pageID
         * @param integer   $pageType      
         * @param string    $title
         * @return string
         */
        public function addPageMap($nodeID,$pageID,$pageType,$title='n/a'){
            //valid URLs out of textual names
            $myTranslit = new translitWrap();
            
            //some bogus check
            if(empty($title)){
                $title = 'n/a';
            }
            
            //prepare variables
            $pageURL = $myTranslit->UrlTranslit($title); //url of the new node
            
            //fix tha ampersand problem
            //if(!$fix){
                //$title   = htmlspecialchars($title);
            //}

			//bad, needs thought
			//$title   = iconv('iso-8859-1', 'utf-8', $title);

            //check for a deformed page url
            if(empty($pageURL)){
                $pageURL = 'n_a';   
            }
            
            //check if the desired pageURL is already taken
            /*
                This code could be made "prettier" in terms, that not the page ID is appended but a
                sequential string // ak
            */
            if($this->db->getOne("SELECT id FROM pagemap WHERE nodeid = ".(int)$nodeID." AND pageurl = '".$pageURL."'")){
                $pageURL .= '_' . (int)$pageID;
            }
            
            //log action
            if(isset($this->log)){
                $this->log->recordAction("Adding new page \"$pageURL\" to parent node \"$nodeID\"");
            }
            
            //figure out group ID and user ID
            if($_SESSION['admin']){
                $gid = $_SESSION['admin']['gid'];
                $uid = $_SESSION['admin']['id'];
            }else{
                $gid = 1000;    // www group
                $uid = 1000;    // www user
            }
            
            //now do the insertion!
            $this->db->Execute("INSERT INTO pagemap(nodeid,pageurl,pageid,pagetype,title, owner_gid, owner_uid) VALUES(
                                                                            '".(int)$nodeID."',
                                                                            '".$pageURL."',
                                                                            '".(int)$pageID."',
                                                                            '".(int)$pageType."',
                                                                            '".$title."',
                                                                            ".$gid.",
                                                                            ".$uid."
                                                                        )");

            return $pageURL;
        }
        
        
        
        /**
         * Updates the title of a selected page
         *
         * @param integer $id
         * @param string $title
         */
        public function updatePage($id,$title){
            //some bogus check
            if(empty($title)){
                $title = 'n/a';
            }
            //echo $title;exit;
            $title = str_replace('&','&amp;',$title);
            //echo $title;
            //$string = iconv('utf-8','iso-8859-1',$item['name']);
            if($GLOBALS['_CONFIG']['locale'] == 'de_AT.UTF-8'){
                //$string   = iconv('utf-8','iso-8859-1//IGNORE',$title);
                $string = $title;
                //echo $string;
                $string   = str_split($string);
                $toReturn = array();
                foreach($string as $key => $val){
                    $t = ord($val);
                    switch($t){
                        case 252: $toReturn[] = '&uuml;'; break;
                        case 246: $toReturn[] = '&ouml;'; break;
                        case 228: $toReturn[] = '&auml;'; break;
                        default:  $toReturn[] = $val;
                    }
                    
                    //echo ord($val).'-'.$val.' ';
                }
                $title   = implode('',$toReturn);
            }
            //echo $title;
            
            //fix tha ampersand problem and get rid of special chars (if any)
            //$title   = htmlspecialchars($title);

            //bad, needs thought
            //$title   = iconv('iso-8859-1', 'utf-8', $title);

            $this->db->Execute("UPDATE pagemap SET title =".$this->db->qstr($title)." WHERE id = ".$id);
        }
        
        
        /**
         * Sets a page to state 'published'
         *
         * @param unknown $id
         */
        public function publishPage($id){
            $this->db->Execute("UPDATE pagemap SET wf_state = 1 WHERE id = ".$id);  
        }
        
        
        /**
         * Sets a page state to 'unpublished'
         *
         * @param unknown $id
         */
        public function unpublishPage($id){
            $this->db->Execute("UPDATE pagemap SET wf_state = null WHERE id = ".$id);  
        }
    }
?>